package com.tianshuo.controller;

import com.google.gson.Gson;
import com.tianshuo.model.User;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tianshuo
 */
@Component("/user")
public class UserController implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User> list = new ArrayList<>();
        User user = new User("1","tianshuo",30);
        User user1 = new User("2","tianyulin",3);
        User user2 = new User("3","tianyuhao",5);
        User user3 = new User("4","liuyang",31);
        list.add(user);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(list));
    }
}
